home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 25 / Cream of the Crop 25.iso / program / gmp202.zip / mpbsd / add.c next >
C/C++ Source or Header  |  1997-04-18  |  3KB  |  121 lines

  1. /* mpz_add -- Add two integers.
  2.  
  3. Copyright (C) 1991, 1993, 1994, 1996 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Library General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
  15. License for more details.
  16.  
  17. You should have received a copy of the GNU Library General Public License
  18. along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  20. MA 02111-1307, USA. */
  21.  
  22. #include "gmp.h"
  23. #include "gmp-impl.h"
  24.  
  25. #ifndef BERKELEY_MP
  26. void
  27. #if __STDC__
  28. mpz_add (mpz_ptr w, mpz_srcptr u, mpz_srcptr v)
  29. #else
  30. mpz_add (w, u, v)
  31.      mpz_ptr w;
  32.      mpz_srcptr u;
  33.      mpz_srcptr v;
  34. #endif
  35. #else /* BERKELEY_MP */
  36. void
  37. #if __STDC__
  38. madd (mpz_srcptr u, mpz_srcptr v, mpz_ptr w)
  39. #else
  40. madd (u, v, w)
  41.      mpz_srcptr u;
  42.      mpz_srcptr v;
  43.      mpz_ptr w;
  44. #endif
  45. #endif /* BERKELEY_MP */
  46. {
  47.   mp_srcptr up, vp;
  48.   mp_ptr wp;
  49.   mp_size_t usize, vsize, wsize;
  50.   mp_size_t abs_usize;
  51.   mp_size_t abs_vsize;
  52.  
  53.   usize = u->_mp_size;
  54.   vsize = v->_mp_size;
  55.   abs_usize = ABS (usize);
  56.   abs_vsize = ABS (vsize);
  57.  
  58.   if (abs_usize < abs_vsize)
  59.     {
  60.       /* Swap U and V. */
  61.       {const __mpz_struct *t = u; u = v; v = t;}
  62.       {mp_size_t t = usize; usize = vsize; vsize = t;}
  63.       {mp_size_t t = abs_usize; abs_usize = abs_vsize; abs_vsize = t;}
  64.     }
  65.  
  66.   /* True: ABS_USIZE >= ABS_VSIZE.  */
  67.  
  68.   /* If not space for w (and possible carry), increase space.  */
  69.   wsize = abs_usize + 1;
  70.   if (w->_mp_alloc < wsize)
  71.     _mpz_realloc (w, wsize);
  72.  
  73.   /* These must be after realloc (u or v may be the same as w).  */
  74.   up = u->_mp_d;
  75.   vp = v->_mp_d;
  76.   wp = w->_mp_d;
  77.  
  78.   if ((usize ^ vsize) < 0)
  79.     {
  80.       /* U and V have different sign.  Need to compare them to determine
  81.      which operand to subtract from which.  */
  82.  
  83.       /* This test is right since ABS_USIZE >= ABS_VSIZE.  */
  84.       if (abs_usize != abs_vsize)
  85.     {
  86.       mpn_sub (wp, up, abs_usize, vp, abs_vsize);
  87.       wsize = abs_usize;
  88.       MPN_NORMALIZE (wp, wsize);
  89.       if (usize < 0)
  90.         wsize = -wsize;
  91.     }
  92.       else if (mpn_cmp (up, vp, abs_usize) < 0)
  93.     {
  94.       mpn_sub_n (wp, vp, up, abs_usize);
  95.       wsize = abs_usize;
  96.       MPN_NORMALIZE (wp, wsize);
  97.       if (usize >= 0)
  98.         wsize = -wsize;
  99.     }
  100.       else
  101.     {
  102.       mpn_sub_n (wp, up, vp, abs_usize);
  103.       wsize = abs_usize;
  104.       MPN_NORMALIZE (wp, wsize);
  105.       if (usize < 0)
  106.         wsize = -wsize;
  107.     }
  108.     }
  109.   else
  110.     {
  111.       /* U and V have same sign.  Add them.  */
  112.       mp_limb_t cy_limb = mpn_add (wp, up, abs_usize, vp, abs_vsize);
  113.       wp[abs_usize] = cy_limb;
  114.       wsize = abs_usize + cy_limb;
  115.       if (usize < 0)
  116.     wsize = -wsize;
  117.     }
  118.  
  119.   w->_mp_size = wsize;
  120. }
  121.